package com.rambo.androidnews.fraction;

import com.rambo.androidnews.ArticleDetailAbility;
import com.rambo.androidnews.itemprovider.ArticleItemProvider;
import com.rambo.androidnews.mvp.IWanAdView;
import com.rambo.androidnews.mvp.WanAdPresenter;
import com.rambo.baselib.ResourceTable;
import com.rambo.baselib.bean.LoadDataConfig;
import com.rambo.baselib.constant.ParamConstant;
import com.rambo.baselib.fraction.BaseFraction;
import com.rambo.baselib.fraction.BaseRefreshListFraction;
import com.rambo.baselib.model.wanad.WanAdApiArticle;
import com.rambo.baselib.model.wanad.WanAdApiPage;
import com.rambo.baselib.model.wanad.WanAdApiResult;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.util.Loading;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.app.Context;

import java.util.List;


/***********************************************************************
 * <p>@description: 玩Android 网站的首页tab
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/13 16:07
 **********************************************************************/
public class WanAdHomeFraction extends BaseRefreshListFraction<IWanAdView, WanAdPresenter> implements IWanAdView {
    ArticleItemProvider articleItemProvider;

    public WanAdHomeFraction(Context context) {
        super(context);
    }


    @Override
    protected BaseItemProvider getItemProvider() {
        articleItemProvider = new ArticleItemProvider(this, null);
        return articleItemProvider;
    }

    @Override
    protected void loadData(LoadDataConfig refreshConfig) {
        presenter.getHomeArticle(loadDataConfig);
    }

    @Override
    protected void registerClick() {
        super.registerClick();
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                WanAdApiArticle wanAdApiArticle = articleItemProvider.getItem(i);
                Intent intent = new Intent();
                Operation operation =
                        new Intent.OperationBuilder()
                                .withBundleName(getBundleName())
                                .withAbilityName(ArticleDetailAbility.class.getName())
                                .build();
                intent.setOperation(operation);
                intent.setParam(ParamConstant.LINK_URL, wanAdApiArticle.getLink());
                startAbility(intent,0);
            }
        });
    }

    @Override
    protected WanAdPresenter createPresenter() {
        return new WanAdPresenter();
    }


    @Override
    public void loadDataSuccess(WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>> data, String apiType) {
        if (data == null ||
                data.getData() == null ||
                data.getData().getDatas() == null ||
                data.getData().getDatas().size() == 0) {
            loading.updateLoadState(Loading.LoadingState.EMPTY);
            loadDataFinish(false, false);
            return;
        }
        loadDataFinish(true, !data.getData().isOver());
        if (loadDataConfig.isLoadingMore) {
            articleItemProvider.add(data.getData().getDatas());
            return;
        }
        articleItemProvider.replace(data.getData().getDatas());
    }

    @Override
    public void loadDataSuccess(WanAdApiResult<List<WanAdApiArticle>> data) {

    }

    @Override
    public void loadDataFailure() {
        loadDataFinish(false, true);
        if (!loadDataConfig.isLoadingMore) {
            loading.updateLoadState(Loading.LoadingState.FAIL);
        }
    }
}
